package net.i4q.sqlved.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Vector;

public class DBConnection {
	private Connection connection = null;
	private Properties props = null;

	public Connection getConnection() {
		return connection;
	}

	public void initConnection(Properties props) throws ClassNotFoundException,
			SQLException {
		this.props = props;
		Class.forName("com.mysql.jdbc.Driver");
		String hostname = props.getProperty("hostname");
		String port = props.getProperty("port");
		port = port==null||port.trim().equals("")?"3306":port;
		String user = props.getProperty("user");
		String password = props.getProperty("password");
		String url = "jdbc:mysql://" + hostname + ":" + port + "/";
		connection = DriverManager.getConnection(url, user, password);
	}
	
	private Vector<String> getAllDatabases() throws SQLException {
		Vector<String> catalogVector = new Vector<String>();
		Connection con = this.getConnection();
		DatabaseMetaData dmd = con.getMetaData();
		ResultSet rs = dmd.getCatalogs();
		while(rs.next()) {
			catalogVector.add(rs.getString(1));
		}
		
		return null;
	}

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException, FileNotFoundException, IOException {
		DBConnection dbConnection = new DBConnection();
		Properties props = new Properties();
		props.load(new FileInputStream("jdbc.properties"));
		dbConnection.initConnection(props);
		ResultSet rs = dbConnection.getConnection().createStatement().executeQuery("select * from va.data_dict where domain='Adult' order by sequence");
		while(rs.next()) {
			System.out.println(rs.getString(6));
		}
	}
}
